<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :node-data="company" :is-root="true" @openDialog="showAddDeptDialog" />
        <el-tree :data="departs" :props="defaultProps">
          <template #default="{data}">
            <!-- 当el-row放到其他标签中,宽度是通过内容来撑开的 -->
            <treeTools
              :node-data="data"
              @del-depts="getDepartmentList"
              @openDialog="showAddDeptDialog"
              @editDialog="showEditDeptDialog"
            />
          </template>
        </el-tree>
      </el-card>
      <addDepts
        ref="addDepts"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :depart-list="departList"
        @add-depts-success="getDepartmentList"
      />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartmentApi } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: { treeTools, addDepts },
  data() {
    return {
      departs: [
        {
          name: '总裁办',
          manager: '曹操',

          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      departList: [],
      // id为空 让所有一级部门都是他的子部门
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      nodeData: {},
      loading: false // 用来控制进度弹层的显示和隐藏
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.loading = true
      const { data } = await getDepartmentApi()
      console.log('获取架构列表成功', data)
      this.company.name = data.companyName
      this.departList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.loading = false
    },
    showAddDeptDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // 当打开弹窗的时候立即获取员工基本信息 或者用open事件
      // this.$refs.addDepts.getSimpleList()
      // this.$refs.addDepts.showTitle = '添加子部门'
    },
    showEditDeptDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // Vue dom是异步的 不能立刻 要等dom更新完毕才调用
      // this.$refs.addDepts.showTitle = '修改部门'
      this.$nextTick(() => {
        this.$refs.addDepts.showEditDepartmentApi()
      })
    }

  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
